package com.github.freewu.mvp.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.github.freewu.mvp.view.status.StatusController;

/**
 * Created by free on 16-7-20.
 */
public abstract class StatusView extends FrameLayout implements StatusController{
    protected View normalView;
    protected View loadingView;
    protected View errorView;
    protected View reloadView;

    public StatusView(Context context) {
        super(context);

        init();
    }

    public StatusView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();
    }

    public StatusView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    /**
     * 返回加载视图
     *
     * @return
     */
    public abstract View getLoadingView();

    /**
     * 返回错误视图
     *
     * @return
     */
    public abstract View getErrorView();

    /**
     * 返回重新加载视图
     * @return
     */
    public abstract View getReloadView();

    public abstract void onCreate();

    public abstract void onDestroy();

    /**
     * 返回正常视图
     *
     * @return
     */
    public View getNormalView(){
        return normalView;
    }

    /**
     * 显示加载视图
     */
    public void showLoadingView() {
        normalView.setVisibility(View.GONE);
        errorView.setVisibility(View.GONE);
        reloadView.setVisibility(View.GONE);
        loadingView.setVisibility(View.VISIBLE);
    }

    /**
     * 显示错误视图
     */
    public void showErrorView(){
        reloadView.setVisibility(View.GONE);
        normalView.setVisibility(View.GONE);
        errorView.setVisibility(View.VISIBLE);
        loadingView.setVisibility(View.GONE);
    }

    /**
     * 显示正常视图
     */
    public void showNormalView(){
        reloadView.setVisibility(View.GONE);
        normalView.setVisibility(View.VISIBLE);
        errorView.setVisibility(View.GONE);
        loadingView.setVisibility(View.GONE);
    }

    /**
     * 显示重新加载视图
     */
    public void showReloadView(){
        reloadView.setVisibility(View.VISIBLE);
        normalView.setVisibility(View.GONE);
        errorView.setVisibility(View.GONE);
        loadingView.setVisibility(View.GONE);
    }

    public void setNormalView(View normalView) {
        this.normalView = normalView;

        this.removeAllViews();

        init();
    }

    private void init(){
        errorView=getErrorView();

        loadingView=getLoadingView();

        reloadView=getReloadView();

        this.addView(errorView);

        this.addView(loadingView);

        this.addView(reloadView);

        if (normalView!=null) {
            this.addView(normalView);

            showNormalView();
        }
    }
}
